const { ccclass, property } = cc._decorator;
import o from "./PlayerDataSys"
import i from "./PeripheralSystemDataMgr"
import r from "./Handler"
import n from "./frameworkManager"
import l from "./GameService"
import s from "./EngineUtil"
import c, { SkillType } from "./MatchGameData"
import u, { VideoBuriedPoint } from "./PeripheralSystemMgr"
import GameService from "./GameService";
import Handler from "./Handler";

@ccclass
class MatchGameSystemMgr {

    static _instance = null;

    static _getInstance() {
        if (!MatchGameSystemMgr._instance) {
            MatchGameSystemMgr._instance = new MatchGameSystemMgr();
        }
        return MatchGameSystemMgr._instance;
    }

    public matchGameStart(gameData, successCallback, errorCallback) {
        l.matchGameStart(gameData, r.create(this, (response) => {
            console.log("开始游戏---", JSON.stringify(response));
            if (!response.code) {
                let temp = JSON.parse(JSON.stringify(response))
                response = {};
                response.code = 1;
                response.data = temp;
            }
            if (response && 1 == response.code) {
                try {
                    let data = response.data
                    // , obj = (data.cash_balance, data.game_level, data.gold_balance, data.profile);
                    // data.props, data.ad_flag, data.full_ad_flag, obj.EVENT_TYPE, obj.LEVEL_CODE, obj.LEVEL_STEEPS,
                    //     obj.REWARD_MONEY, obj.STARTGUIDE_TYPE;
                    console.log(2222232233223232, JSON.stringify(data))
                    c.setGameData(data);
                } catch (error) {
                    console.error(error)
                }
                successCallback && successCallback();
            } else if (response.message) {
                s.showManageViewToast(response.message);
            }
        }), r.create(this, function () {
            s.showManageViewToast("网络异常，请检查网络");
            errorCallback && errorCallback();
        }))
    };


    public addPropVideo(e, t) {
        var a = {
            video_type: e,
            is_video: true,
            step: 0,
            is_tg: 0
        };
        GameService.addProp({
            prop: e,
            count: e == 2 ? 5 : 3
        },Handler.create(this, () => {
            t && t();
        }), Handler.create(this, (e) => {
            console.error("获取广告配置数据失败", e);
        }))
       
        e == SkillType.Hammer ? i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_HAMMER) : e == SkillType.Arrow ? i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_BOW_AND_ARROW) : e == SkillType.Gun && i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_ARTILLERY);
        return;
        o.getUserCpm() && Object.assign(a, o.getUserCpm());
        n.log("addPropVideo====cpm", o.getUserCpm());
        l.matchGameVideo(a, r.create(this, function (a) {
            n.log("看视频得道具---", a);
            if (a && 1 == a.code) {
                var o = a.data.props;
                c.setPropInfo(o);
                e == SkillType.Hammer ? i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_HAMMER) : e == SkillType.Arrow ? i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_BOW_AND_ARROW) : e == SkillType.Gun && i.reportVideoBuriedPoint(VideoBuriedPoint.OBTAIN_ARTILLERY);
                t && t();
            } else a.message && s.showManageViewToast(a.message);
        }), r.create(this, function () {
            s.showManageViewToast("网络异常，请检查网络");
        }));
    };

    public matchGameFail(e, t) {
        l.submitGame({
            step: e,
            is_tg: 0
        }, r.create(this, (e) => {
            n.log("看视频得道具---", e);
            if (e && 1 == e.code) {
                var a = e.data, o = a.props;
                c.setPropInfo(o);
                i.setTravelData(a);
                t && t();
            } else e.message && s.showManageViewToast(e.message);
        }), r.create(this, function () {
            s.showManageViewToast("网络异常，请检查网络");
        }));
    };

    public matchGameSuccess(e, t) {
        l.submitGame({
            step: e
        }, r.create(this, (e) => {
            n.log("游戏通关---", e);
            var a = e.result;
            c.setSubmitData(a);
            i.setTravelData(a);
            e && 1 == e.code ? t && t(a) : e.message && s.showManageViewToast(e.message);
        }), r.create(this, function () {
            s.showManageViewToast("网络异常，请检查网络");
        }));
    };

    public addStepVideo(e, t) {
        var a = {
            video_type: 6,
            is_video: true,
            step: e,
            is_tg: 0
        };
        o.getUserCpm() && Object.assign(a, o.getUserCpm());
        n.log("addStepVideo====cpm", o.getUserCpm());
        // l.matchGameVideo(a, r.create(this, (e) => {
        //     n.log("看视频得道具---", e);
        //     if (e && 1 == e.code) {
                c.addStep();
                i.reportVideoBuriedPoint(VideoBuriedPoint.ADD_STEP);
                t && t();
            // } else e.message && s.showManageViewToast(e.message);
        // }), r.create(this, function () {
        //     s.showManageViewToast("网络异常，请检查网络");
        // }));
    };

    public addRewardVideo(e) {
        var t = {
            video_type: 7,
            is_video: true,
            step: 0,
            is_tg: 0
        };
        o.getUserCpm() && Object.assign(t, o.getUserCpm());
        n.log("addRewardVideo====cpm", o.getUserCpm());
        // l.matchGameVideo(t, r.create(this, function (t) {
            n.log("关卡内红包奖励---", t);
            // var a = t.data;
            // if (t && 1 == t.code) {
                // c.addFlyBoxCash(a.cash);
                c.addFlyBoxCash(1);
                e && e();
            // } else t.message && s.showManageViewToast(t.message);
        // }), r.create(this, function () {
        //     s.showManageViewToast("网络异常，请检查网络");
        // }));
    };

    public successDouble(e, t, a) {
        void 0 === t && (t = false);
        var p = {
            video_type: 5,
            is_video: true,
            step: e,
            is_tg: 1,
            update_flag: t
        };
        o.getUserCpm() && Object.assign(p, o.getUserCpm());
        n.log("successDouble====cpm", o.getUserCpm());
        // l.matchGameVideo(p, r.create(this, (e) => {
        //     n.log("看视频得道具---", e);
        //     if (e && 1 == e.code) {
                c.addStep();
                var t = e.data, o = t.cash, r = t.travel_reward_info;
                t.gold;
                i.setRewardCash(o);
                i.isHitOppoReviewBan() && i.setRewardGold(0);
                i.setTravelRewardInfo(r);
                i.reportVideoBuriedPoint(VideoBuriedPoint.DOUBLE_RED_PAPER);
                a && a();
        //     } else e.message && s.showManageViewToast(e.message);
        // }), r.create(this, function () {
        //     s.showManageViewToast("网络异常，请检查网络");
        // }));
    };

    public useProp(e, t) {
        l.useProp({
            prop: e,
            num: 1
        }, r.create(this, (e) => {
            n.log("使用道具---", e);
            if (e && 0 == e.code) {
                var a = e.result.props;
                c.setPropInfo(a);
                t && t();
            } else e.message && s.showManageViewToast(e.message);
        }), r.create(this, function () {
            s.showManageViewToast("网络异常，请检查网络");
        }));
    };
}

const a = MatchGameSystemMgr._getInstance();

export default a;